package com.szl.sbssh.service.impl;

import com.szl.sbssh.jpa.SysDictionaryDetailRepository;
import com.szl.sbssh.model.TSysDictionaryDetail;
import com.szl.sbssh.pageModel.base.PageFilter;
import com.szl.sbssh.pageModel.base.SessionInfo;
import com.szl.sbssh.pageModel.base.Tree;
import com.szl.sbssh.pageModel.sys.SysDictionaryDetail;
import com.szl.sbssh.service.SysDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: songzhonglin
 * Date: 2018/4/25
 * Time: 15:47
 * Description:
 **/
@Service
@Transactional
public class SysDictionaryDetailServiceImpl implements SysDictionaryDetailService {

    @Autowired
    private SysDictionaryDetailRepository sysDictionaryDetailRepository;

    @Override
    public List<SysDictionaryDetail> dataGrid(SysDictionaryDetail sysDictionaryDetail, PageFilter ph, SessionInfo sessionInfo) {
        return null;
    }

    @Override
    public Long count(SysDictionaryDetail sysDictionaryDetail, PageFilter ph, SessionInfo sessionInfo) {
        return null;
    }

    @Override
    public void add(SessionInfo sessionInfo, SysDictionaryDetail sysDictionaryDetail) {

    }

    @Override
    public void delete(Long id, HttpServletRequest request) {

    }

    @Override
    public void edit(SessionInfo sessionInfo, SysDictionaryDetail sysDictionaryDetail) {

    }

    @Override
    public SysDictionaryDetail get(Long id) {
        return null;
    }

    @Override
    public boolean dictionaryExists(String code) {
        return false;
    }

    @Override
    public List<Tree> getDictionaryByParentCode(String parentCode) {
        List<TSysDictionaryDetail> l = sysDictionaryDetailRepository.findAll(new Specification<TSysDictionaryDetail>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<TSysDictionaryDetail> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p1 = cb.equal(root.get("parentCode").as(String.class), parentCode);
                query.where(cb.and(p1));
                query.orderBy(cb.asc(root.get("sequence").as(Integer.class)));
                return query.getRestriction();
            }
        });
        List<Tree> lt = new ArrayList<Tree>();
        if ((l != null) && (l.size() > 0)) {
            for (TSysDictionaryDetail r : l) {
                Tree tree = new Tree();
                tree.setId(r.getCode());
                tree.setText(r.getName());
                lt.add(tree);
            }
        }
        return lt;
    }
}
